//
//  BackPOSHisSVC.m
//  NewPro
//
//  Created by 何荧红 on 2019/3/26.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "BackPOSHisSVC.h"

#import "POSWithNameCell.h"

#import "PostlistModel.h"
#import "POSStore.h"

@interface BackPOSHisSVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, assign) NSInteger VCType;

@property (nonatomic, strong) ViewFoot *foot;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, assign) BOOL isTimesort;
@property (nonatomic, copy) NSString *timesort;

@end

@implementation BackPOSHisSVC

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self GetPostlist];
}
- (void)initCSS{
    self.VCType = 11;
    self.view.backgroundColor = [UIColor colorWithHexString:@"F4F4F4"];
    self.foot = [[ViewFoot alloc]initWithPOSRecordFoot];
    self.foot.MyPOSListType = self.VCType;
    [self.view addSubview:_foot];
    self.timesort = @"";
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, iPhoneX_Top + 25, SCREEN_WIDTH, SCREEN_HEIGHT - iPhoneX_Top - 55) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[POSWithNameCell class] forCellReuseIdentifier:@"POSWithNameCellID"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        [self GetPostlist];
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.bottom.equalTo(self.foot.mas_top);
    }];
}
- (void)GetPostlist{
    [[[POSStore alloc]init] GetBackFlowHistoryWithPrams:@{@"customerId":[UserDefaultsManager sharedInstance].user_id,@"type":@"2"} Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            NSArray *arr = res[@"data"];
            if (arr.count > 0) {
                NSInteger allnumber = 0;
                NSInteger succ = 0;
                for (NSDictionary *dic in arr) {
                    PostHisModel *model = [PostHisModel mj_objectWithKeyValues:dic];
                    model.HeaderISPush = NO;
                    allnumber += [model.count integerValue];
                    succ += [[NSString stringWithFormat:@"%@",dic[@"successCount"]] integerValue];
                    [self.dataArr addObject:model];
                }
                self.foot.allnumber = allnumber;
                self.foot.SecondPosnumber = @"";
                self.foot.ThridPosnumber = [NSString stringWithFormat:@"%ld",succ];
            }else{
                [self showMBPError:@"暂无数据"];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)GetlistData:(PostHisModel *)model{
    NSMutableDictionary * prams = [[NSMutableDictionary alloc]init];
    [prams setObject:@"2" forKey:@"type"];
    [prams setObject:@"1" forKey:@"current"];
    [prams setObject:model.count forKey:@"size"];
    [prams setObject:[UserDefaultsManager sharedInstance].user_id forKey:@"customerId"];
    if (self.isTimesort) {
        [prams setObject:self.timesort forKey:@"descs"];
    }else{
        [prams setObject:self.timesort forKey:@"ascs"];
    }
    [[[POSStore alloc]init] GetBackFlowHistoryDetailWithPrams:prams andID:model.requestId Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            NSArray *arr = res[@"data"][@"records"];
            if (arr.count > 0) {
                NSMutableArray *subArr = [[NSMutableArray alloc]init];
                NSInteger allclik = 0;
                for (NSDictionary *dic in arr) {
                    PostHisDetailModel *model = [PostHisDetailModel mj_objectWithKeyValues:dic];
                    if ([model.status isEqualToString:@"1"]) {
                        allclik += 1;
                    }
                    [subArr addObject:model];
                }
                model.infoArr = subArr;
            }else{
                [self showMBPError:@"暂无数据"];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
    } Failure:^(NSError * _Nonnull error) {
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArr.count > 0) {
        PostHisModel *model = self.dataArr[indexPath.section];
        if (model.HeaderISPush) {
            CGFloat height = 70;
            if (model.infoArr.count > 0) {
                height = 70 + 20 * model.infoArr.count;
            }
            return height;
        }else{
            return 40;
        }
    }
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
    foot.backgroundColor = [UIColor clearColor];
    return foot;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    POSWithNameCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"POSWithNameCellID"];
    Cell.CellType = self.VCType;
    if (self.dataArr.count > 0) {
        PostHisModel *model = self.dataArr[indexPath.section];
        Cell.contstring = model.realName;
        Cell.phonestring = model.phoneNumber;
        Cell.numberstr = [model.count integerValue];
        if (model.infoArr.count > 0) {
            Cell.dataArr = model.infoArr;
        }
        [Cell setTabPushBlock:^(BOOL push) {
            PostHisModel *model = self.dataArr[indexPath.section];
            model.HeaderISPush = push;
            if (push) {
                [self GetlistData:model];
            }else{
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }
        }];
        Cell.isPush = model.HeaderISPush;
    }
    Cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return Cell;
}

@end
